package com.neuedu.hw_20190215;

import java.util.Scanner;

public class Hw06 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入邮箱地址：");
        String str = scanner.next();

        if (error(str)==true){
            print(str);
        }else {
            System.out.println("不合法");
        }
    }
    public static boolean error(String email){
        boolean m = true;
        if (email.indexOf('@')==-1||email.indexOf('.')==-1){
            System.out.println("邮箱中不包含@或.");
            return false;
        }
        if (email.indexOf('@') != email.lastIndexOf('@')||email.indexOf('.') != email.lastIndexOf('.')){
            System.out.println("邮箱中包含两个或两个以上的@或.");
            return false;
        }
        if (email.indexOf('@') > email.indexOf('.')){
            System.out.println("邮箱中.出现在了@前面");
            return false;
        }
        if (email.charAt(0) >= '0' && email.charAt(0) <= '9'){
            System.out.println("邮箱开头为字母");
            return false;
        }
        for (int i = 0; i <email.length() ; i++) {
            if (email.charAt(i) >= 'a' && email.charAt(i) <= 'z' ||
                    email.charAt(i) >= '0' && email.charAt(i) <= '9' || email.charAt(i) == '_' ||
                    email.charAt(i) == '@'|| email.charAt(i) == '.'){

            }else{
                System.out.println("邮箱中包含非法字符");
                return false;
            }

            if (email.charAt(i) == '_'){
                if (email.charAt(i+1) == '_'){
                    System.out.println("邮箱中包含连续的下划线");
                    return false;
                }
            }else {

            }
        }
        return true;
    }
    public static void print(String email){
        String [] str = email.split("[@]");
        System.out.println("用户名："+str[0]+"\n网站域名:"+str[1]);
    }
}
